package vd1;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class GameGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel lblCell;
	private GameInfo info;
	private JButton btnFirstPlayer;
	private JButton btnSecondPlayer;

	public GameInfo getInfo() {
		return info;
	}

	public void setInfo(GameInfo info) {
		this.info = info;
	}

	/**
	 * Create the frame.
	 */
	public GameGUI(GameInfo info) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		ClickButtonListener ac = new ClickButtonListener();
		
		btnFirstPlayer = new JButton("A");
		contentPane.add(btnFirstPlayer, BorderLayout.NORTH);
		btnFirstPlayer.addActionListener(ac);

		btnSecondPlayer = new JButton("B");
		contentPane.add(btnSecondPlayer, BorderLayout.SOUTH);
		btnSecondPlayer.addActionListener(ac);

		lblCell = new JLabel("");
		lblCell.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblCell, BorderLayout.CENTER);
		this.info = info;
		init();
	}

	private void init() {
		updateGround();
		lblCell.setText("");
	}
	
	private void updateGround(){
		int currentPlayer = info.getPlayer();
		if (currentPlayer == 1) {
			btnFirstPlayer.setEnabled(true);
			btnSecondPlayer.setEnabled(false);
		} else {
			btnSecondPlayer.setEnabled(true);
			btnFirstPlayer.setEnabled(false);
			
		}
	}
	
	class ClickButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(info.getPlayer()==1){
				lblCell.setText("X");
				info.setPlayer(2);
			}else{
				lblCell.setText("0");
				info.setPlayer(1);
			}
			updateGround();
		}
		
	}

}
